<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class login_controller extends admin_controller
{
    public function index()
    {
        if($this->is_admin_logged()) redirect(base_url(). $this->base_admin_url .'user_controller');
        $data['show_menu'] = 'false';
        $data['action'] = base_url().'login/check-login';
        $data['theme_path'] = base_url();
        $data['base_admin_url'] = 'ci-admin/';
        $data['menu_item'] = $this->get_menu_item();
        $data['can_apply_tinymce'] = $this->can_apply_tinymce();
        $this->load->view("admin/master_page/header",$data);
        $this->load->view("admin/login",$data);
        $this->load->view("admin/master_page/footer",$data);
    }

    public function initialize_menu()
    {
        return array();
    }

    public function check_login()
    {
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $user = $this->user_model->get_element_by_username_and_password($username,$password);
        if($user->id != '')
        {
            $this->set_admin_logged($user->id);
            redirect(base_url(). $this->base_admin_url . 'user_controller');
        }
        else
        {
            $this->unset_admin_logged();
            $data['message'] = 'Sai tên đăng nhập hoặc mật khẩu.';
            $data['action'] = base_url().'login/check-login';
            $data['theme_path'] = base_url();
            $data['base_admin_url'] = 'ci-admin/';
            $data['can_apply_tinymce'] = $this->can_apply_tinymce();
            $this->load->view("admin/master_page/header",$data);
            $this->load->view("admin/login",$data);
            $this->load->view("admin/master_page/footer",$data);
        }
    }

    public function logout()
    {
        $this->unset_admin_logged();
        redirect(base_url().'ci-admin');

    }

    public function can_this_user_access_this_page()
    {
        return true;
    }
}